package com.tinem.platform.service.network.api;

import cn.hutool.core.bean.BeanUtil;
import com.tinem.platform.module.pojo.vo.network.ip.IpInfoReq;
import com.tinem.platform.module.pojo.vo.network.ip.IpInfoRes;
import com.tinem.platform.module.starter.jpa.entity.network.IpInfo;
import com.tinem.platform.module.starter.jpa.repository.network.IpInfoRepository;
import com.tinem.platform.service.network.client.IpInfoClient;
import io.netty.util.NetUtil;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author fzh
 * @version v1
 * @program: platform
 * @className IpApi
 * @description TODO
 * @site 
 * @company 
 * @create 2020-08-25 16:10
 */
@Slf4j
@Api(value = "ip")
@RestController
@RequestMapping("/ip")
public class IpApi {

    @Resource
    IpInfoRepository ipInfoRepository;

    @Resource
    IpInfoClient databaseIpInfoClient;

    @Resource
    IpInfoClient ip2regionIpInfoClient;

    @Resource
    IpInfoClient tengXunIpInfoClient;

    @Resource
    IpInfoClient ipRangeInfoClient;


    @GlobalTransactional
    @PostMapping("/locat.api")
    @ApiOperation(value = "通过ip 获取地理位置信息", httpMethod = "POST", notes = "暂无")
    public IpInfoRes info(@RequestBody IpInfoReq req) {
        IpInfo ipInfo = null;
        IpInfoRes ipInfoRes = new IpInfoRes();
        //如果启用缓存，则从数据缓存中获取。
        if("enable".equals(req.getCache())){
            ipInfo = databaseIpInfoClient.getLocationByIp(req.getIp());
            if(ipInfo != null){
                BeanUtil.copyProperties(ipInfo,ipInfoRes);
                return ipInfoRes;
            }
        }
        //如果是ipv4则先从本地数据库文件中获取ip地址信息。
        if("enable".equals(req.getIp2region())){
            if(NetUtil.isValidIpV4Address(req.getIp())){
                ipInfo = ip2regionIpInfoClient.getLocationByIp(req.getIp());
            }

            if(ipInfo == null){
                // 从mysql数据库读取
                ipInfo = ipRangeInfoClient.getLocationByIp(req.getIp());
            }
        }
        //如果无法从本地数据库文件中获取ip地址信息，则使用第三方api。
        if(ipInfo==null){
            switch (req.getType()){
                case "ip138":
                    ipInfo = tengXunIpInfoClient.getLocationByIp(req.getIp());
                    break;
                case "tengxun":
                    ipInfo = tengXunIpInfoClient.getLocationByIp(req.getIp());
                    break;
                default:
                    log.error("ip type（{}） is error",req.getType());
                    return null;
            }
        }
        if(ipInfo!=null){
            //保存ip信息到数据库
            try {
                ipInfoRepository.saveAndFlush(ipInfo);
            }catch (Exception e){
                log.error("ip({}) save to db is error",ipInfo.getIp());
            }
        }
        BeanUtil.copyProperties(ipInfo,ipInfoRes);
        return ipInfoRes;
    }
}
